<template>
  <div>
    <!-- 查询框 -->
    <el-form :inline="true" :model="queryForm" class="demo-form-inline">

      <el-form-item label="房源类型">
        <el-select v-model="queryForm.houseType" placeholder="楼盘类型" clearable>
          <el-option v-bind:label="item.name" :value="item.code" v-for="item in houseTypeList"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="房源编码" >
        <el-input v-model="queryForm.houseNumber" placeholder="房源编码" cclearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="querySubmit">查询</el-button>
      </el-form-item>
    </el-form>
    <!-- 列表 -->
    <el-table
        :data="tableData"
        @selection-change="handleSelectionChange"
        style="width: 90%">
      <!-- 多选
      <el-table-column
      type="selection"
      ></el-table-column>-->

      <el-table-column width="50"
          label="#" prop="houseId"
      ></el-table-column>

      <el-table-column width="100"
          prop="estateName"
          label="所属楼盘">
      </el-table-column>

      <el-table-column width="100"
          prop="houseType"
          label="房源类型">
        <template slot-scope="scope">
          <span v-for="item in houseTypeList" v-if="item.code==scope.row.houseType">{{item.name}}</span>
        </template>
      </el-table-column>

      <el-table-column width="100"
          prop="houseNumber"
          label="房源编码">
      </el-table-column>


      <el-table-column label="Img" prop="elevator" width="50">
        <template slot-scope="scope">
          <img :src="scope.row.houseImg" width="30px;">
        </template>
      </el-table-column>

      <el-table-column label="朝向" prop="orientation" width="50">
      </el-table-column>

      <el-table-column width="100"
          prop="structure"
          label="户型结构">
      </el-table-column>
      <el-table-column width="50"
          prop="buildingFloor"
          label="楼层">
      </el-table-column>

      <el-table-column label="配套设施" prop="facilityNames" width="160">
      </el-table-column>

      <el-table-column label="电梯" prop="elevator" width="50">
        <template slot-scope="scope">
          {{scope.row.elevator==1?'是':'否'}}
        </template>
      </el-table-column>

      <el-table-column label="建筑面积" prop="buildingArea" width="100">
      </el-table-column>
      <!-- 分页 -->
      <el-table-column label="操作" width="250">
        <template slot-scope="scope">
          <el-button size="mini" @click="edit(scope.row)">编辑</el-button>
          <el-button size="mini" type="primary" @click="check(scope.row)">审核</el-button>
          <el-button size="mini" type="primary" @click="pulished(scope.row)">发布</el-button>
        </template>
      </el-table-column>

    </el-table>

    <el-pagination
        :page-count="pageCount"
        :current-page="queryForm.current"
        @current-change="gotoPage"
        layout="prev, pager, next">
    </el-pagination>


    <el-row :gutter="20">
      <el-col :span="16"><div class="grid-content bg-purple">
        <el-button size="mini" @click="add">添加</el-button>
      </div></el-col>
      <el-col :span="8"><div class="grid-content bg-purple">
        <el-button size="mini" @click="syncAll">全量同步</el-button>
      </div></el-col>
    </el-row>
    <div>
      <!-- <el-button size="mini" type="danger" @click="batchDel()">批删</el-button> -->

    </div>


    <!-- 对话框添加房源信息 -->
    <el-dialog
        title="房源(添加/编辑)"
        :visible.sync="dialogVisible"
        width="40%"
        :before-close="handleClose">
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="房源名称">
          <el-input v-model="form.houseNumber" placeholder="请输入名称"></el-input>
        </el-form-item>
        <el-form-item label="房源编码">
          <el-input v-model="form.estateId" placeholder="请输入名称"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
		    <el-button @click="dialogVisible = false">取 消</el-button>
		    <el-button type="primary" @click="save">确 定</el-button>
		  </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data(){
    return {
      //查询条件
      queryForm:{
        pageNum:1,
        pageSize:3
      },
      //列表数据
      tableData:[],
      //table选择数据Id数组
      selecteIds:[],
      //总页数
      pageCount:1,
      dialogVisible:false,
      form:{},
      houseTypeList:[
      {code:'jiZhShi', name: '集中式'},
      {code:'fenSanShi', name: '分散式'},
    ]
    }
  },
  methods:{
    handleClose() {

    },
    save(){
      //ajax提交房源信息
      this.axios.post("house/save",this.form).then(res=>{
        if(res.data.result){
          //关闭对话框
          this.form={};
          this.dialogVisible = false;
          this.$message({
            message:"添加成功",
            type:"success"
          });
          this.querySubmit();
        }else {
          this.$message(res.data.message);
        }
      })
    },
    add(){
      this.$router.push("/update");
    },
    check(row){
      this.axios.get("house/check",{params: {houseId:row.houseId}}).then(res=>{
        this.$message.success(res.data.msg);
        this.list();
      })
    },
    pulished(row) {
      this.axios.get("house/pulished",{params: {houseId:row.houseId}}).then(res=>{
        this.$message.success(res.data.msg);
        this.list();
      })
    },
    syncAll() {
      this.$message.success("开始啦");
      this.axios.get("house/all").then(res=>{
        this.$message.success(res.data.msg);
      })
    },
    edit(row){
      this.$router.push("/update?id=" + row.houseId);
    },
    handleSelectionChange(rows){
      console.log(rows);
      //let ids = rows.map(function(row){return row.id});
      let ids = rows.map(row=>{return row.id});
      this.selecteIds = ids;
      console.log("selecteIds:"+this.selecteIds);
    },
    gotoPage(current){
      this.queryForm.pageNum = current;
      this.list();
    },
    querySubmit(){
      //发送ajax请求，根据queryForm查询数据
      console.log(this.queryForm);
      this.queryForm.pageNum=1;
      this.list();

    },
    //列表页数据
    list(){
      this.axios.get("house/page",{params:this.queryForm}).then(res=>{
        this.tableData = res.data.data.list;
        this.pageCount = res.data.data.pages;
      })
    }
  },
  created(){
    this.list();
  }
}
</script>

<style scoped>

</style>
